﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Data;
using NLBS.Framework;

namespace NLBS.Web
{
   public class Cookie
    {
       public static void AddCookie(string key,string mode)
       {
           HttpContext context= HttpContext.Current;
           if (HttpContext.Current.Request.Cookies[key] != null)
           {
               context.Response.Cookies.Remove(key);
           }
           HttpCookie modeCookie = new HttpCookie(key, mode);
           modeCookie.Expires = DateTime.Now.AddDays(1);
           context.Response.Cookies.Add(modeCookie);
           context.Request.Cookies[key].Value = mode;
       }

       public static void SetModeCookie()
       {
           if (BlogInfo.Instance().DefaultViewMode == 0)
           {
               SetQueryCookie(StateKey.ArticleModeCookieKey, StateKey.QueryStringMode, "normal");
           }
           else
           {
               SetQueryCookie(StateKey.ArticleModeCookieKey, StateKey.QueryStringMode, "list");
           }
       }

       public static void SetSkinCookie(string value)
       {
           HttpContext context = HttpContext.Current;
           if (context.Request.Cookies[StateKey.SkinCookieKey] == null)
           {
               HttpCookie modeCookie = new HttpCookie(StateKey.SkinCookieKey, value);
               modeCookie.Expires = DateTime.Now.AddDays(1);
               context.Response.Cookies.Add(modeCookie);
           }
           else
           {
               Cookie.AddCookie(StateKey.SkinCookieKey, value);
           }
       }

       public static void SetQueryCookie(string key,string query,string defaultValue)
       {
           
           HttpContext context = HttpContext.Current;
           if (context.Request.Cookies[StateKey.ArticleModeCookieKey] == null)
           {
               HttpCookie modeCookie = new HttpCookie(key, defaultValue);
               modeCookie.Expires = DateTime.Now.AddDays(1);
               context.Response.Cookies.Add(modeCookie);
           }
           else if (context.Request.QueryString[query] != null)
           {
               Cookie.AddCookie(key, context.Request.QueryString[query].ToString());
           }
       }
    }
}
